//
//  MineCommentCell.m
//  ShowYork
//
//  Created by hxh on 2020/11/16.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "MineCommentCell.h"
#import "MineAtUserInfoModel.h"
#import "TTTAttributedLabel.h"
@interface MineCommentCell ()<TTTAttributedLabelDelegate,UIGestureRecognizerDelegate>
@property (nonatomic,strong)UILabel *nameLb;
@property (nonatomic,strong)UILabel *dateLb;
@property (nonatomic,strong)UILabel *remindLb;
@property (nonatomic,strong)UIImageView *headerIcon;
@property (nonatomic,strong)UIImageView *thumbnailIcon;
@property (nonatomic,strong)UIView *bageView;
@property (nonatomic,strong)TTTAttributedLabel *contentLb;
@property (nonatomic,strong)UIView *line;
@property (nonatomic,strong)UIImageView *videoIcon;
@property (nonatomic,strong)UIView *postContentView;
@property (nonatomic,strong)UILabel *postContentLb;
@property (nonatomic,strong)UIView *removeView;
@property (nonatomic,strong)UILabel *removeLb;
@property (nonatomic,strong)UIButton *coverBtn;
@property (nonatomic,strong)UIButton *contentCoverBtn;
@property (nonatomic,strong)UIButton *thumbnailBtn;
@property (nonatomic,strong)UIButton *contentBtn;
@end

@implementation MineCommentCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setUI];
    }
    return self;
}
- (void)setUI{
    
    self.coverBtn = [[UIButton alloc]init];
    [self.coverBtn addTarget:self action:@selector(contentCoverBtnMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.coverBtn];
    
    [self.coverBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.contentView);
    }];
    
    self.headerIcon = [[UIImageView alloc]init];
    self.headerIcon.layer.cornerRadius = 20;
    self.headerIcon.layer.masksToBounds = YES;
    [self.contentView addSubview:self.headerIcon];
    
    [self.headerIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.left.equalTo(self.contentView).offset(16);
        make.size.mas_equalTo(CGSizeMake(40, 40));
    }];
    
    self.bageView = [[UIView alloc]init];
    self.bageView.backgroundColor = RGBA(232, 85, 83, 1);
    self.bageView.layer.cornerRadius = 3;
    self.bageView.layer.masksToBounds = YES;
    [self.contentView addSubview:self.bageView];
    [self.bageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.left.equalTo(self.contentView).offset(5);
        make.size.mas_equalTo(CGSizeMake(6, 6));
    }];
    self.nameLb = [[UILabel alloc]init];
    self.nameLb.textColor = C51;
    self.nameLb.font = [UIFont boldSystemFontOfSize:14];
    [self.contentView addSubview:self.nameLb];
    [self.nameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(16);
        make.left.equalTo(self.headerIcon.mas_right).offset(16);
    }];
    
    self.thumbnailIcon = [[UIImageView alloc]init];
    self.thumbnailIcon.backgroundColor = RGBA(240, 240, 240, 1);
    self.thumbnailIcon.contentMode = UIViewContentModeScaleAspectFill;
    self.thumbnailIcon.layer.masksToBounds = YES;
    [self.contentView addSubview:self.thumbnailIcon];
    [self.thumbnailIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(21);
        make.right.equalTo(self.contentView).offset(-16);
        make.size.mas_equalTo(CGSizeMake(50, 50));
    }];
    
    self.contentLb = [[TTTAttributedLabel alloc]initWithFrame:CGRectZero];
    self.contentLb.textColor = C102;
    self.contentLb.delegate = self;
    self.contentLb.numberOfLines = 0;
    self.contentLb.font = [UIFont systemFontOfSize:14];
    self.contentLb.linkAttributes = @{(NSString *)kCTForegroundColorAttributeName:[RPAppManager themColor],
                                    (NSString *)kCTUnderlineStyleAttributeName:[NSNumber numberWithBool:NO]};
    [self.contentView addSubview:self.contentLb];
    
    self.contentLb.text = @"@";

    
    [self.contentLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headerIcon.mas_right).offset(16);
        make.right.equalTo(self.thumbnailIcon.mas_left).offset(-8);
        make.top.equalTo(self.nameLb.mas_bottom).offset(4);
    }];
    
    
    self.postContentView = [[UIView alloc]init];
    self.postContentView.backgroundColor = RGBA(240, 240, 240, 1);
    [self.contentView addSubview:self.postContentView];
    
    self.postContentLb = [[UILabel alloc]init];
    self.postContentLb.textColor = C102;
    self.postContentLb.numberOfLines = 4;
    self.postContentLb.font = font(12);
    [self.postContentView addSubview:self.postContentLb];
    
 
    [self.postContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.thumbnailIcon);
    }];
    
    [self.postContentLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.postContentView);
    }];
    
    self.videoIcon = [[UIImageView alloc]init];
    self.videoIcon.image = [UIImage imageNamed:@"mine_video_tag"];
    [self.contentView addSubview:self.videoIcon];
    [self.videoIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.thumbnailIcon);
    }];
    
    self.remindLb = [[UILabel alloc]init];
    self.remindLb.textColor = C153;
    self.remindLb.font = font(10);
    [self.contentView addSubview:self.remindLb];
    [self.remindLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLb);
        make.top.equalTo(self.contentLb.mas_bottom).offset(4);
        make.bottom.equalTo(self.contentView).offset(-16);
    }];
    
    
    self.line = [[UIView alloc]init];
    self.line.backgroundColor = RGBA(230, 230, 230, 1);
    [self.contentView addSubview:self.line];
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.right.mas_equalTo(self.contentView);
        make.left.equalTo(self.nameLb);
        make.height.mas_equalTo(0.5);
    }];
    
    self.removeView = [[UIView alloc]init];
    self.removeView.backgroundColor = RGBA(240, 240, 240, 1);
    [self.contentView addSubview:self.removeView];
    [self.removeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.left.equalTo(self.contentLb);
        make.width.mas_equalTo(50);
        make.height.mas_equalTo(20);
    }];
    self.removeLb = [[UILabel alloc]init];
    self.removeLb.text = @"已删除";
    self.removeLb.textColor = RGBA(153, 153, 153, 1);
    self.removeLb.font = font(10);
    [self.removeView addSubview:self.removeLb];
    [self.removeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.removeView);
    }];
    
    self.thumbnailBtn = [[UIButton alloc]init];
    [self.thumbnailBtn addTarget:self action:@selector(contentCoverBtnMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.thumbnailBtn];
    [self.thumbnailBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.thumbnailIcon);
    }];
    

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapMethod)];
    tap.delegate = self;
    [self.contentLb addGestureRecognizer:tap];
    tap.cancelsTouchesInView = NO;
    
    self.contentCoverBtn = [[UIButton alloc]init];
    [self.contentCoverBtn addTarget:self action:@selector(contentCoverBtnMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.contentLb addSubview:self.contentCoverBtn];
    [self.contentCoverBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.contentLb);
    }];
    
    self.headerIcon.userInteractionEnabled = YES;
    self.nameLb.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *headerIconTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showUserInfo)];
    UITapGestureRecognizer *nameLbTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showUserInfo)];
    [self.headerIcon addGestureRecognizer:headerIconTap];
    [self.nameLb addGestureRecognizer:nameLbTap];
    
    
    self.thumbnailIcon.userInteractionEnabled = YES;
    self.postContentView.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *removeTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(contentCoverBtnMethod)];
    [self.removeView addGestureRecognizer:removeTap];
    
}

- (void)setModel:(MineMessageItemModel *)model{
    _model = model;
    self.nameLb.text = model.sendUserName;
    if (model.imagePath.length == 0) {
        if (model.sex.intValue == 0) {
            self.headerIcon.image = [UIImage imageNamed:@"defaut_header"];
        }else if (model.sex.intValue == 1){
            self.headerIcon.image = [UIImage imageNamed:@"common_boy_tag"];
        }else{
            self.headerIcon.image = [UIImage imageNamed:@"common_girl_tag"];
        }
    }else{
        [self.headerIcon sd_setImageWithURL:[NSURL URLWithString:model.imagePath] placeholderImage:[UIImage imageNamed:@"defaut_header"]];
    }
    if (model.commentState.intValue == 1) {
        self.contentLb.text = @" ";
        self.removeView.hidden = NO;
    }else{
        self.removeView.hidden = YES;
        self.contentLb.text = model.commentText;
        NSString *commentContent = model.commentText;
        if (model.atUserList.count > 0) {
            for (MineAtUserInfoModel *userInfo in model.atUserList) {
                NSRange range = [commentContent rangeOfString:[NSString stringWithFormat:@"{%@}",userInfo.id]];
                if (range.location != NSNotFound) {
                    commentContent = [commentContent stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"{%@}",userInfo.id] withString:[NSString stringWithFormat:@"@%@",userInfo.realName.length == 0?@"":userInfo.realName]options:0 range:range];
                    userInfo.location = range.location;
                }

            }
            self.contentLb.text = commentContent;
            for (MineAtUserInfoModel *userInfo in model.atUserList) {
                if (userInfo.location != -1) {
                    NSRange selRange = NSMakeRange(userInfo.location,userInfo.realName.length + 1);
                    [self.contentLb addLinkToTransitInformation:@{@"data":userInfo.id} withRange:selRange];
                }
            }
        }
    }
    
    if (model.unread.intValue == 1) {
        self.bageView.hidden = YES;
    }else{
        self.bageView.hidden = NO;
    }
    self.videoIcon.hidden = YES;
    self.postContentLb.text = model.postText;
    self.postContentView.hidden = YES;
    if(model.postType.intValue == 1){
        self.thumbnailIcon.hidden = YES;
        self.postContentView.hidden = NO;
        self.postContentLb.text = model.postText;
    }
    if (model.postType.intValue == 2) {
        self.postContentLb.text = @"";
        self.thumbnailIcon.hidden = NO;
        [self.thumbnailIcon sd_setImageWithURL:[NSURL URLWithString:model.postUrl]];
    }
    if (model.postType.intValue == 3) {
        self.postContentLb.text = @"";
        self.videoIcon.hidden = NO;
        self.thumbnailIcon.hidden = NO;
        [self.thumbnailIcon sd_setImageWithURL:[NSURL URLWithString:model.postUrl]];
    }
    
    // 评论
    if (model.messageType.intValue == 3) {
        if (model.contentType.intValue == 1) {
            self.remindLb.text = [NSString stringWithFormat:@"评论了你的动态 %@",model.createStr];
        }else if(model.contentType.intValue == 2){
            self.remindLb.text = [NSString stringWithFormat:@"评论了你的作品 %@",model.createStr];
        }else if(model.contentType.intValue == 5){
            self.remindLb.text = [NSString stringWithFormat:@"回答了你的问题 %@",model.createStr];
        }
    }
    // 回复
    if (model.messageType.intValue == 4) {
        self.remindLb.text = [NSString stringWithFormat:@"回复了你的评论 %@",model.createStr];
    }
}

+(instancetype)cellWithTableView:(UITableView *)tableView{
    NSString *ID = @"MineCommentCell";
    MineCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[MineCommentCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithTransitInformation:(NSDictionary *)components {
    NSNumber *userId = components[@"data"];
    if (self.delegate && [self.delegate respondsToSelector:@selector(showUserInfoById:)]) {
        [self.delegate showUserInfoById:userId];
    }
}

- (void)tapMethod{
    
}

- (void)coverMethod{
    
}

- (void)contentCoverBtnMethod{
    if (self.delegate && [self.delegate respondsToSelector:@selector(showCommentOrReplyByItemModel:)]) {
        [self.delegate showCommentOrReplyByItemModel:self.model];
    }
}

- (void)showUserInfo{
    if (self.delegate && [self.delegate respondsToSelector:@selector(showUserInfoById:)]) {
        [self.delegate showUserInfoById:self.model.sendUserId];
    }
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
        // 过滤掉UIButton，也可以是其他类型
    if ( [touch.view isKindOfClass:[TTTAttributedLabel class]])
    {
        return NO;
    }
    
    return YES;
}

@end
